<?php
error_reporting(0);
ini_set('memory_limit', '1000M');
set_time_limit(0);


/** 
 *  mapper_7.php
 *      input:格式：mapred3输出 (json1\tjson2\t...)尤其要注意其中的weight字段
		1.对比上 	 weight>=35
		5.仅模糊对比上   1<=weight<35
		7.独立房源       weight=0   
		8.异常数据    （第一轮处理）
 *      handle:生成sql
 *      output:sql语句--INSERT INTO `house_sell_gov` VALUES ('1', ...);
 * @created 2016-03-09
   @updated 
 * @by lily
 */
$city=$argv[1];
$borough_info_file_name = $city."/borough_info";
$borough_price_file_name = $city."/borough_price";
$subwaytoborough_filename = $city."/subwaytoborough";
$subwaytoborough_file = file($subwaytoborough_filename);
foreach($subwaytoborough_file as &$line_json){
    $line = json_decode($line_json, True);
    $subwaytoborough[$line['bid']] = $line['id'];
}
$borough_info_file = file($borough_info_file_name);
$borough_price_file = file($borough_price_file_name);
$borough_info_keys = array('borough_shop', 'middle_school', 'elementary_school', 'nursery_school', 'borough_hospital');
foreach($borough_info_file as &$line_json){
    $line = json_decode($line_json, True);
    $borough_info = array();
    foreach($borough_info_keys as $key)
        $borough_info[$key] = $line[$key];
    $borough_info_tbl[$line['id']] = $borough_info;
}
foreach ($borough_price_file as &$line_json){
    $line = json_decode($line_json, True);
    $borough_price[$line['borough_id']] = $line['price'];
}

$nu=0;
while (($line_json = fgets(STDIN)) !== false) {   //line_json=>source_url\tjson格式
    $line_json = trim($line_json);
    if (empty($line_json))
        continue;
    //list ($unique_id,$min_price, $cnt, $deviation, $list_json) = explode("\t", $line_json);
		if($nu==0) {
			echo "INSERT INTO house_sell_gov(id,broker_id,house_price,house_desc,house_title,cityarea_id,cityarea2_id,house_floor,house_topfloor,house_toward,house_room,house_hall,house_toilet,house_kitchen,house_fitment,house_feature,house_built_year,use_area,owner_name,owner_phone,service_phone,house_support,created,updated,status,source,source_owner,app_url,wap_url,source_url,is_checked,click_num,refresh,house_type,house_totalarea,borough_id,borough_name,house_pic_unit,house_pic_layout,house_number,is_fill,is_contrast,public_time,source_name,tag) VALUES";
			$nu=1;
		}  
		else{
			echo ",\n";
			$nu+=1;
		} 
		
		$one_dejson=json_decode($line_json,true); 
        $is_subway = $subwaytoborough[$one_dejson['borough_id']];
        $one_dejson['tag'] = str_replace("12#", "", $one_dejson['tag']);
        if(!empty($is_subway)){
            $one_dejson['tag'] = $one_dejson['tag'].'12#';
            $line_json = json_encode($one_dejson);
        }
        else if(strstr($one_dejson['tag'], "地铁房")){
            $one_dejson['tag'] = str_replace("地铁房", "12", $one_dejson['tag']);
        }

        $one_dejson['tag'] = str_replace("1001#", "", $one_dejson['tag']);
        if ($one_dejson['house_totalarea'] > 0){
            $hprice=$one_dejson['house_price']/$one_dejson['house_totalarea'];
            if ($borough_price[$one_dejson['borough_id']]){
                $bprice=$borough_price[$one_dejson['borough_id']];
                $price_ratio =$one_dejson['house_price']/$one_dejson['house_totalarea'] * 10000 / $bprice;
            if ($hprice > 30 || $hprice < 0.3 || $price_ratio > 2 || $price_ratio < 0.5){
                $one_dejson['tag'] = "1001#".$one_dejson['tag'];
            }
            }
            else if ($hprice > 30 || $hprice < 0.3){
                $one_dejson['tag'] = "1001#".$one_dejson['tag'];
            }
        }
        if(empty($one_dejson['house_desc']) || $one_dejson['house_desc']==""){
            $borough_info = $borough_info_tbl[$one_dejson['borough_id']];
            $borough_desc = array();
            if($borough_info){
                $borough_desc["附近有超市商场"] = $borough_info['borough_shop'];
                $borough_desc["附近有医院"] = $borough_info['borough_hospital'];
                $borough_desc["附近有学校"] = implode(" ",array($borough_info['nursery_school'],$borough_info['middle_school'],$borough_info['elementary_school']));
            }
            if($one_dejson['source'] == 10){
                $house_desc = "我是业主，我的房子在".$one_dejson['borough_name'].",";
                foreach($borough_desc as $desc=>$desc_data){
                    $desc_data=trim($desc_data);
                    if(!empty($desc_data)){
                        $house_desc = $house_desc.$desc.",".$desc_data.",";
                    }
                }
                $house_desc .= "希望把房子交给有缘人。";
            }else{
                $sourcename = $rent_config["ARRAY_GOV_SOURCE"];
                $house_desc = "我是".$sourcename[$one_dejson['source']]."的一名经纪人，竭诚为您服务，这套房子在".$one_dejson['borough_name']."。";
                foreach($borough_desc as $desc=>$desc_data){
                 $desc_data=trim($desc_data);
                 if(!empty($desc_data)){
                        $house_desc = $house_desc.$desc.",".$desc_data.",";
                    }
                }
                $house_desc .= "我保证信息的真实，并期待您和我联系!";
            }
            $one_dejson['house_desc'] = $house_desc;
        }
        #continue;    

		
		echo " ('".$one_dejson['id']."','"
		.$one_dejson['broker_id']."','".$one_dejson['house_price']."','".
		str_replace("'","",str_replace("\\","",$one_dejson['house_desc']))."','".
		str_replace("'","",str_replace("\\","",$one_dejson['house_title']))."','".
		$one_dejson['cityarea_id']."','".$one_dejson['cityarea2_id']."','".
		$one_dejson['house_floor']."','".$one_dejson['house_topfloor']."','".
		$one_dejson['house_toward']."','".$one_dejson['house_room']."','".
		$one_dejson['house_hall']."','".$one_dejson['house_toilet']."','".
		$one_dejson['house_kitchen']."','".$one_dejson['house_fitment']."','".
		str_replace("'","",$one_dejson['house_feature'])."','".
		str_replace("'","",$one_dejson['house_built_year'])."','".
		$one_dejson['use_area']."','".
		str_replace("'","",$one_dejson['owner_name'])."','".
		$one_dejson['owner_phone']."','".
		$one_dejson['service_phone']."','".$one_dejson['house_support']."','".
		$one_dejson['created']."','".$one_dejson['updated']."','".
		$one_dejson['status']."','".$one_dejson['source']."','".
		$one_dejson['source_owner']."','".$one_dejson['app_url']."','".
		$one_dejson['wap_url']."','".$one_dejson['source_url']."','".
		$one_dejson['is_checked']."','".$one_dejson['click_num']."','".
		$one_dejson['refresh']."','".$one_dejson['house_type']."','".
		$one_dejson['house_totalarea']."','".$one_dejson['borough_id']."','".
		$one_dejson['borough_name']."','".$one_dejson['house_pic_unit']."','".
		$one_dejson['house_pic_layout']."','".$one_dejson['house_number']."','".
		$one_dejson['is_fill']."','".$is_contrast."','".
		$one_dejson['public_time']."','".$one_dejson['source_name']."','".$one_dejson['tag']."')";	
		if($nu==19){
		    echo ";\n";
		    $nu=0;
		}
}
echo ";\n";
?>

